'All code written by Andrew Lorimer for E-Tech Software Imports System.Text.RegularExpressions Public Class stopwatch 'DECLERATION OF VARIABLES Public startTime As System.DateTime 'Stores when the timer was started Public elapsedTime As System.TimeSpan 'Stores the elapsed time Public hasBeenPaused As Boolean = False 'Determines whether the time has been paused Public startPause As System.DateTime 'Determines when the user pressed pause Public elapsedPause As System.TimeSpan 'How long the current session of paused time has been going for Public elapsedPauseAddends As System.TimeSpan 'How long other sessions of paused time have been going for Public hasAlerted As Boolean = False 'Determines whether the timer has alerted the user Public firstStart As Boolean = False 'Is this the first form? Public alertTime As TimeSpan = TimeSpan.Parse("00:05:00.0") 'Time to alert the user #Region "UI Events" Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click reset() End Sub Private Sub KryptonButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton4.Click 'Show/hide the log If Me.Size.Width = 650 Then Me.MinimumSize = New Size(0, 0) Me.MaximumSize = New Size(0, 0) Me.Width = 397 Me.MinimumSize = New Size(397, 286) Me.MaximumSize = New Size(397, 286) KryptonButton4.Text = "Show Log" Else Me.MinimumSize = New Size(0, 0) Me.MaximumSize = New Size(0, 0) Me.Width = 650 Me.MinimumSize = New Size(650, 286) Me.MaximumSize = New Size(650, 286) KryptonButton4.Text = "Hide Log" KryptonListBox1.Focus() End If End Sub Private Sub KryptonButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton5.Click 'Add a logged time My.Settings.logID = My.Settings.logID + 1 KryptonListBox1.Items.Add("Log " & My.Settings.logID.ToString & " | " & Label1.Text) Me.MinimumSize = New Size(0, 0) Me.MaximumSize = New Size(0, 0) Me.Width = 650 Me.MinimumSize = New Size(650, 286) Me.MaximumSize = New Size(650, 286) KryptonButton4.Text = "Hide Log" KryptonListBox1.Focus() End Sub Private Sub timer_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 'Save the log My.Settings.logStopwatch.Clear() For Each item In KryptonListBox1.Items My.Settings.logStopwatch.Add(item) Next End Sub Private Sub timer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Starts timing if nessecary If My.Settings.autoTime Then start(TimeSpan.Parse("00:00:00.0")) End If End Sub Private Sub KryptonButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton6.Click 'Starts at the selected time If Not KryptonListBox1.SelectedItem = "" Then Dim strArr() As String strArr = KryptonListBox1.SelectedItem.Split("| ") reset() start(TimeSpan.Parse(strArr(1).ToString)) End If End Sub Private Sub KryptonButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton7.Click 'Remove the selected logged time Dim lst As New List(Of Object) For Each a As Object In KryptonListBox1.SelectedItems lst.Add(a) Next For Each a As Object In lst KryptonListBox1.Items.Remove(a) Next End Sub Private Sub KryptonButton8_Click(sender As Object, e As EventArgs) Handles KryptonButton8.Click 'Shows the settings dialog settings.Show() settings.BringToFront() End Sub Private Sub NotifyIcon1_Click(sender As Object, e As EventArgs) Handles NotifyIcon1.Click 'Focus the window when the Notify Icon is clicked Me.Focus() End Sub Private Sub KryptonButton10_Click(sender As Object, e As EventArgs) Handles KryptonButton10.Click 'Shows the countdown window and closes the stopwatch window countdown.Show() countdown.Location = New Point(Me.Location.X, Me.Location.Y) countdown.Focus() My.Settings.Save() Me.Close() End Sub Private Sub KryptonTextBox1_TextChanged(sender As Object, e As EventArgs) Handles KryptonTextBox1.TextChanged 'Synchronises the label text and form text with the text in the textbox Label1.Text = KryptonTextBox1.Text Label1.Left = (198.5) - (Label1.Width / 2) Me.Text = KryptonTextBox1.Text End Sub Private Sub stopwatch_Shown(sender As Object, e As EventArgs) Handles Me.Shown 'Check if this is the right form If My.Settings.startupMode = "c" And My.Settings.firstStart = False Then countdown.Show() countdown.Location = New Point(Me.Location.X, Me.Location.Y) countdown.Focus() Me.Close() End If My.Settings.firstStart = True 'Loads the log and other values For Each item In My.Settings.logStopwatch KryptonListBox1.Items.Add(item) Next If My.Settings.hideExtra = True Then Label1.Text = "00:00:00.0" Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form Me.Text = Label1.Text 'Update form text Else Label1.Text = "00:00:00.00000" Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form Me.Text = Label1.Text 'Update form text End If If My.Settings.updateCheck = False Then BackgroundWorker1.RunWorkerAsync() My.Settings.updateCheck = True End If If My.Settings.showLogDefault = True Then Me.MinimumSize = New Size(0, 0) Me.MaximumSize = New Size(0, 0) Me.Width = 650 Me.MinimumSize = New Size(650, 286) Me.MaximumSize = New Size(650, 286) KryptonButton4.Text = "Hide Log" End If Me.StartPosition = FormStartPosition.Manual End Sub Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 'Checks for an updated version Try Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://members.optusnet.com.au/filtered/timerVersion.txt") Dim response As System.Net.HttpWebResponse = request.GetResponse() Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream()) Dim version As String = sr.ReadToEnd If version > "5.0" Then Dim result As DialogResult = MessageBox.Show("Version " & version & " is now available. Press OK to download the update.", "New Version Available", MessageBoxButtons.OKCancel) If result = Windows.Forms.DialogResult.OK Then Process.Start("http://etechtimer.codeplex.com/releases/") End If End If Catch ex As Exception End Try End Sub Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click 'Copy current time to clipboard Clipboard.SetText(Label1.Text) NotifyIcon1.ShowBalloonTip(1000, "Copied to clipboard", "The elapsed time has been copied to your clipboard.", ToolTipIcon.Info) End Sub Private Sub KryptonListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles KryptonListBox1.KeyDown 'Delete selected item with DELETE key If e.KeyCode = Keys.Delete Then 'Remove the selected logged time Dim lst As New List(Of Object) For Each a As Object In KryptonListBox1.SelectedItems lst.Add(a) Next For Each a As Object In lst KryptonListBox1.Items.Remove(a) Next End If If e.Control And e.KeyCode.ToString = "A" Then 'Select all logged items For i As Integer = 0 To KryptonListBox1.Items.Count - 1 KryptonListBox1.SetSelected(i, True) Next End If End Sub Private Sub KryptonListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles KryptonListBox1.SelectedIndexChanged 'Disables/enables "Start at Selected" button based on selection If KryptonListBox1.SelectedItems.Count > 1 Then KryptonButton6.Enabled = False Else KryptonButton6.Enabled = True End If End Sub Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click 'Bring the form to the front of screen Me.WindowState = FormWindowState.Normal End Sub Private Sub StartTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartTimingToolStripMenuItem.Click 'Start timing start(TimeSpan.Parse("00:00:00.0")) End Sub Private Sub PauseTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PauseTimingToolStripMenuItem.Click 'Pause timing pause() Me.WindowState = FormWindowState.Normal End Sub Private Sub ResetTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetTimingToolStripMenuItem.Click 'Reset timing reset() 'Focus start button and form KryptonButton9.Focus() KryptonButton9.Text = "Start" Me.WindowState = FormWindowState.Normal End Sub Private Sub LogTimeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogTimeToolStripMenuItem.Click 'Add a logged time KryptonListBox1.Items.Add(Label1.Text) Me.Width = 650 KryptonButton4.Text = "Hide Log" Me.WindowState = FormWindowState.Normal End Sub Private Sub RenameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RenameToolStripMenuItem.Click 'Rename a logged item If Not KryptonListBox1.SelectedItem = "" Then RenameStopwatch.Show() End If End Sub Private Sub StartAtSelectedToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartAtSelectedToolStripMenuItem.Click 'Starts at the selected time If Not KryptonListBox1.SelectedItem = "" Then Dim strArr() As String strArr = KryptonListBox1.SelectedItem.Split("| ") reset() start(TimeSpan.Parse(strArr(1).ToString)) End If End Sub Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click 'Remove the selected logged time If Not KryptonListBox1.SelectedItem = "" Then Dim lst As New List(Of Object) For Each a As Object In KryptonListBox1.SelectedItems lst.Add(a) Next For Each a As Object In lst KryptonListBox1.Items.Remove(a) Next End If End Sub Private Sub KryptonButton9_Click_1(sender As Object, e As EventArgs) Handles KryptonButton9.Click If KryptonButton9.Text = "Start" Or KryptonButton9.Text = "Resume" Then 'Start at: Dim matches As MatchCollection = New Regex("^((\d?\d)?\:(?=\d*\:))?((\d?\d)?\:)?(\d?\d(\.\d*)?)?$").Matches(KryptonTextBox1.Text) Dim output As String Try For Each value As Integer In {2, 4, 5} ' Index of current working value Dim val = matches(0).Groups(value).Value ' Current working value If Not value = 5 Then ' Format hours, minutes If val = "" Then val = "00" End If val = val + ":" Else ' Format seconds If val = "" Then val = "00.0" End If End If output = output + val Next 'Alert at: matches = New Regex("^((\d?\d)?\:(?=\d*\:))?((\d?\d)?\:)?(\d?\d(\.\d*)?)?$").Matches(KryptonTextBox2.Text) Dim output2 As String For Each value As Integer In {2, 4, 5} ' Index of current working value Dim val = matches(0).Groups(value).Value ' Current working value If Not value = 5 Then ' Format hours, minutes If val = "" Then val = "00" End If val = val + ":" Else ' Format seconds If val = "" Then val = "00.0" End If End If output2 = output2 + val Next alertTime = TimeSpan.Parse(output2) start(TimeSpan.Parse(output)) Catch ex As Exception Dim result As DialogResult = MessageBox.Show("Check that 'Start At' and 'Alert At' are in acceptable formats." & Microsoft.VisualBasic.vbNewLine & "Press OK to see acceptable formats.", "Invalid Format", MessageBoxButtons.OKCancel) If result = Windows.Forms.DialogResult.OK Then Process.Start("https://etechtimer.codeplex.com/wikipage?title=Acceptable%20Input%20Formats") End If End Try Else pause() End If End Sub #End Region #Region "Timer Functionality" Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Try 'Happens every 1ms... 'Update elapsed time: elapsedTime = (DateTime.Now - startTime) - elapsedPause If My.Settings.hideExtra = True Then Label1.Text = elapsedTime.ToString.Substring(0, elapsedTime.ToString.IndexOf(".") + 2) 'Hide extra characters Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form Me.Text = Label1.Text 'Update form text NotifyIcon1.Text = elapsedTime.ToString.Substring(0, elapsedTime.ToString.IndexOf(".") + 2) & " - E-Tech Timer" 'Update systray icon Else Label1.Text = elapsedTime.ToString Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form Me.Text = Label1.Text 'Update form text NotifyIcon1.Text = elapsedTime.ToString & " - E-Tech Timer" 'Update systray icon End If 'Check if the program should alert the user: If KryptonCheckBox1.Checked And elapsedTime > alertTime And hasAlerted = False Then If My.Settings.keepCounting = False Then 'Check if the timer should keep going... Timer1.Stop() 'If not, stop the clock KryptonButton9.Text = "Start" End If 'Play and show the alert Dim Sound As New System.Media.SoundPlayer() If My.Settings.alertName = "Horn" Then Sound.Stream = My.Resources.horn ElseIf My.Settings.alertName = "Morning" Then Sound.Stream = My.Resources.morning ElseIf My.Settings.alertName = "Tinkle" Then Sound.Stream = My.Resources.tinkle End If Sound.Load() Sound.Play() NotifyIcon1.ShowBalloonTip(1000, "Time's up! - E-Tech Timer", "The specified time has been reached. Click here to open E-Tech Timer.", ToolTipIcon.Info) hasAlerted = True End If hasBeenPaused = False 'Makes sure that this is false Catch ex As Exception End Try End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick 'Happens every 1ms WHEN THE TIMER IS PAUSED elapsedPause = (DateTime.Now - startPause) + elapsedPauseAddends End Sub Sub start(ByVal inputTime As TimeSpan) 'Start timing Timer2.Stop() If hasBeenPaused = False Then startTime = System.DateTime.Now - inputTime Try If inputTime > alertTime Then KryptonCheckBox1.Checked = False End If Catch End Try Else elapsedPauseAddends = elapsedPause End If Timer1.Start() KryptonButton9.Text = "Pause" End Sub Sub pause() 'Pause timing If Timer1.Enabled = True Then Timer1.Stop() startPause = DateTime.Now Timer2.Start() hasBeenPaused = True End If KryptonButton9.Text = "Resume" End Sub Sub reset() 'Reset timing 'Stop the timers: Timer1.Stop() Timer2.Stop() 'Reset values: elapsedTime = Nothing elapsedPause = Nothing elapsedPauseAddends = Nothing If My.Settings.hideExtra = True Then Label1.Text = KryptonTextBox1.Text Label1.Left = 198.5 - (Label1.Width / 2) 'Center label in form Me.Text = Label1.Text 'Update form text Else Label1.Text = KryptonTextBox1.Text Label1.Left = 198.5 - (Label1.Width / 2) 'Center label in form Me.Text = Label1.Text 'Update form text End If hasBeenPaused = False Label1.Left = 198.5 - (Label1.Width / 2) hasAlerted = False 'Focus start button KryptonButton9.Focus() KryptonButton9.Text = "Start" End Sub #End Region Private Sub KryptonTextBox2_TextChanged(sender As Object, e As EventArgs) Handles KryptonTextBox2.TextChanged 'Interpret input Dim matches As MatchCollection = New Regex("^((\d?\d)?\:(?=\d*\:))?((\d?\d)?\:)?(\d?\d(\.\d*)?)?$").Matches(KryptonTextBox2.Text) Dim output As String Try For Each value As Integer In {2, 4, 5} ' Index of current working value Dim val = matches(0).Groups(value).Value ' Current working value If Not value = 5 Then ' Format hours, minutes If val = "" Then val = "00" End If val = val + ":" Else ' Format seconds If val = "" Then val = "00.0" End If End If output = output + val Next If Not Timer1.Enabled And Not TimeSpan.Parse(output) = TimeSpan.Zero Then alertTime = TimeSpan.Parse(output) End If Catch ex As Exception End Try End Sub Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Me.Focus() End Sub End Class